<?php
  $objXajax->registerFunction("msfrmroomtype_save");
  $objXajax->registerFunction("msfrmroomtype_edit");
  $objXajax->registerFunction("msfrmroomtype_delete");
  $objXajax->registerFunction("msfrmroomtype_loadGrid");
  function msfrmroomtype_save($params){     
    global $objDbUpdate,$tbl_postfix,$user_id;
    $objResponse = new xajaxResponse();
    $name = addslashes($params['txtnametype']);
    $style = addslashes($params['cbbstyle']);
    $description = addslashes($params['description']);
    $low1 = $params['low-1']; 
    $low2 = $params['low-2']; 
    $low3 = $params['low-3']; 
    $low4 = $params['low-4']; 
    $low5 = $params['low-5']; 
    $low6 = $params['low-6']; 
    $low0 = $params['low-0']; 
    
    $high1 = $params['high-1']; 
    $high2 = $params['high-2']; 
    $high3 = $params['high-3']; 
    $high4 = $params['high-4']; 
    $high5 = $params['high-5']; 
    $high6 = $params['high-6']; 
    $high0 = $params['high-0']; 

    $price_1_1= $params['txtprice-1-1']; 
    $price_1_2= $params['txtprice-1-2']; 
    $price_1_3= $params['txtprice-1-3']; 
    $price_2_1= $params['txtprice-2-1']; 
    $price_2_2= $params['txtprice-2-2']; 
    $price_2_3= $params['txtprice-2-3']; 
    $price_3_1= $params['txtprice-3-1']; 
    $price_3_2= $params['txtprice-3-2']; 
    $price_3_3= $params['txtprice-3-3']; 
    $price_4_1= $params['txtprice-4-1']; 
    $price_4_2= $params['txtprice-4-2']; 
    $price_4_3= $params['txtprice-4-3']; 
    $price_5_1= $params['txtprice-5-1']; 
    $price_5_2= $params['txtprice-5-2']; 
    $price_5_3= $params['txtprice-5-3']; 
    $price_6_1= $params['txtprice-6-1']; 
    $price_6_2= $params['txtprice-6-2']; 
    $price_6_3= $params['txtprice-6-3']; 
    $price_0_1= $params['txtprice-0-1']; 
    $price_0_2= $params['txtprice-0-2']; 
    $price_0_3= $params['txtprice-0-3']; 

    $id = $params['hdId'];
    $edit = $params['hdEdit'];
    $flag = true;

    
    if($params['hdEdit']==1){
        $sqlUpdate = "update ka_room_type$tbl_postfix set name ='$name',style='$style',description='$description',
        date_update=now(),user_update='$user_id' WHERE id='$id' ";
        $flag = $objDbUpdate->Execute($sqlUpdate);    
        $sqldelete = "delete from ka_room_price$tbl_postfix WHERE room_type_id = $id" ;
        $objDbUpdate->Execute($sqldelete);    
 
    }else{
        $sqlInsert = "insert into ka_room_type$tbl_postfix(name,style,description,date_created,date_update,user_created,user_update)
        value('$name','$style','$description',now(),now(),'$username','$username')";
        $flag = $objDbUpdate->Execute($sqlInsert);
        $id = mysql_insert_id();
        $objResponse->addScript("changeUpdate('msfrmroomtype',$id);");  
    }
     $sql = "INSERT INTO ka_room_price$tbl_postfix(room_type_id,timefrom,timeto,price,dateofweek) values";
     $sql.="($id,0,$low1,$price_1_1,1)";
     $sql.=",($id,$low1,$high1,$price_1_2,1)";
     $sql.=",($id,$high1,24,$price_1_3,1)";
     
     $sql.=",($id,0,$low2,$price_2_1,2)";
     $sql.=",($id,$low2,$high2,$price_2_2,2)";
     $sql.=",($id,$high2,24,$price_2_3,2)";
     
     $sql.=",($id,0,$low3,$price_3_1,3)";
     $sql.=",($id,$low3,$high3,$price_3_2,3)";
     $sql.=",($id,$high3,24,$price_3_3,3)";
     
     $sql.=",($id,0,$low4,$price_4_1,4)";
     $sql.=",($id,$low4,$high4,$price_4_2,4)";
     $sql.=",($id,$high4,24,$price_4_3,4)";
     
     $sql.=",($id,0,$low5,$price_5_1,5)";
     $sql.=",($id,$low5,$high5,$price_5_2,5)";
     $sql.=",($id,$high5,24,$price_5_3,5)";
     
     $sql.=",($id,0,$low6,$price_6_1,6)";
     $sql.=",($id,$low6,$high1,$price_6_2,6)";
     $sql.=",($id,$high6,24,$price_6_3,6)";
     
     $sql.=",($id,0,$low0,$price_0_1,0)";     
     $sql.=",($id,$low0,$high0,$price_0_2,0)";
     $sql.=",($id,$high0,24,$price_0_3,0)";
     $objDbUpdate->Execute($sql); 

     $objResponse->addScript('xajax_msfrmroomtype_loadGrid();');    
     //$objResponse->addScript('xajax_msfrmmanagerkara_loadGrid();'); 
     //$objResponse->addScript('xajax_htfrmmanagerhotel_Init();');  
      return $objResponse->getXML(); 
  }
  
   function msfrmroomtype_loadGrid(){
    global $lang,$path;
    $objResponse = new xajaxResponse();
    $arrGrid = msfrmroomtype_GetGrid();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/ka_roomtype_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('msfrmroomtype_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('msfrmroomtype_tbl');");
    $objResponse->addScript('changeWaiting(1);');                               
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
  }
  
    function  msfrmroomtype_GetGrid(){
        global $objDbSelect,$tbl_postfix;
        $sqlSelect = "select * from ka_room_type$tbl_postfix  order by name";
        return $objDbSelect->GetArray($sqlSelect);
    }
    function msfrmroomtype_delete($strId){
        global $objDbSelect,$tbl_postfix;
       $objResponse = new xajaxResponse();
       $sql = " delete from ka_room$tbl_postfix where room_type in ($strId) " ;
       if($objDbSelect->Execute($sql)){
           $sql = " delete from ka_room_price$tbl_postfix where room_type_id in ($strId) " ;
           $objDbSelect->Execute($sql); 
           $sql = " delete from ka_room_type$tbl_postfix where id in ($strId) " ; 
           $objDbSelect->Execute($sql);
       }
       $objResponse->addScript('changeWaiting(1);');
       $objResponse->addScript('deleteRowlogic();');
       return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
    }
    
    function msfrmroomtype_edit($id){
       global $objDbSelect,$tbl_postfix;
       $objResponse = new xajaxResponse();
       $sql = " SELECT *,REPLACE(REPLACE(description, '\n\r', ' '),'\n',' ') as description1 FROM ka_room_type$tbl_postfix WHERE id = '$id' ";
      // echo $sql; die();
       $arr = $objDbSelect->GetArray($sql);      
       
      $style        = $arr[0]['style'];
      $description  = $arr[0]['description1'];
      $name         = $arr[0]['name'];                                              
      $objResponse->addScript("$('#msfrmroomtype_txtnametype').val('$name');");
      $objResponse->addScript("$('#msfrmroomtype_cbbstyle-$style').attr('checked',true);");
      $objResponse->addScript("$('#msfrmroomtype_description').val('$description');");
      $sql = " select * from ka_room_price$tbl_postfix where room_type_id = $id order by dateofweek,timefrom "  ;
      $arr = $objDbSelect->GetArray($sql);
      $flg =0;
      $value0 = 6;
      $value1 = 18;
      $price1 = 0;
      $price2 = 0;
      $price3 = 0;
      for($i=0;$i<count($arr);$i++){
          $dateofweek = $arr[$i]['dateofweek'];
          if($flg==0){
              $value0= $arr[$i]['timeto'];
              $price1 = $arr[$i]['price'];
              $flg++;
          }elseif($flg==1){
              $value1= $arr[$i]['timeto'];  
              $price2= $arr[$i]['price'];
              $flg++;  
          }else{
              $price3= $arr[$i]['price'];
              $flg =0;
              $objResponse->addScript("msfrmroomtype_SetSlider($dateofweek,$value0,$value1,$price1,$price2,$price3);");  
          }
      }
      $objResponse->addScript("changeCssEdit('sss');");
      $objResponse->addScript('changeWaiting(1);');
      return $objResponse->getXML();   
    }
?>
